package com.suse.ecommerce.controller;

import com.suse.ecommerce.pojo.User;
import com.suse.ecommerce.service.UserService;
import com.suse.ecommerce.uilts.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private  UserService userService;
    @RequestMapping("/checkLoginInfo")
    public Msg checkLoginInfo(@RequestParam(value="phone")String phone,
                              @RequestParam(value="password")String password, HttpServletRequest request){
        User user = userService.checkLoginInfo(phone, password);
        HttpSession session = request.getSession();
        if(user!=null){
            session.setAttribute("user",user);
            return Msg.success("登录成功");
        }else{
            return Msg.error("登录失败，用户名或者密码错误");
        }
    }
    @RequestMapping("/getUserById")
    public Msg getUserById(User user){
        if(user.getUserId() == null){
            return Msg.error("没有此人");
        }else {
            return Msg.success("成功").add("user",userService.getUseById(user.getUserId()));
        }
    }
}
